﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GamePlugin.Logics.Core;
using WindowsInput;
using System.Drawing;
using GamePlugin.Logics.Data;
using System.Drawing.Imaging;
using GamePlugin.Logics.Data.Auction;
using GamePlugin.IO;
using System.Windows.Media;
using System.Windows;

namespace GamePlugin.Logics.Game.Actions
{
    public class DActionAuctionGrabData : MachineState
    {
        public override void Initialize()
        {
            this.AddState("GrabData");
            this["GrabData"].UpdateEvent += new EventHandler<GameTimeEventArgs>((o, e) => this.GrabData(o, e));

            this.DefaultState = "GrabData";
        }

        public void GrabData(object o, GameTimeEventArgs e)
        {
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;


            if (input.InputDeviceState.IsHardwareKeyDown(WindowsInput.Native.VirtualKeyCode.F1))
            {
                (this.Owner[typeof(DActionAuctionStart).Name] as DActionAuctionStart).IsGrabDataMode = false;
                this.Owner.IsNeedReset = true;
                this.Owner.ChangeToState(typeof(DActionAuctionStart).Name);
            }

            if (input.InputDeviceState.IsHardwareKeyDown(WindowsInput.Native.VirtualKeyCode.F2))
            {
                Helper.ScreenChecker.GetScreenData();

                MessageBox.Show("开始记录竞拍目标");
                var card = GetIdentityCard();

                //var card = ImageStorage.GetBitmap("Test.bmp");

                if (card != null)
                {
                    AuctionItem auctionItem = new AuctionItem();

                    auctionItem.ImagePath = IOManager.Instance.GenerateDataPathName();
                    auctionItem.IsExactDeadline = false;
                    auctionItem.ImageSource = Helper.GetImageSource(card);
                    auctionItem.AuctionPrice = 0;
                    auctionItem.AuctionDeadline = DateTime.Now.AddHours(36);

                    card.Save(auctionItem.ImagePath);

                    Workspace.Instance.AuctionItemList.Children.Add(auctionItem);

                    Helper.GetMainWindow().AuctionControl.AuctionItemListBox.DataContext = Workspace.Instance.AuctionItemList;
                    Helper.GetMainWindow().AuctionControl.AuctionItemListBox.SelectedIndex = Workspace.Instance.AuctionItemList.Children.Count - 1;
                }

                MessageBox.Show("结束记录竞拍目标");
            }
        }

        private Bitmap GetIdentityCard()
        {
            Bitmap ret = null;

            Bitmap leftUpCorner = ImageStorage.GetBitmap("LeftUpCorner.bmp");
            Bitmap rightBottomCorner = ImageStorage.GetBitmap("RightBottomCorner.bmp");

            Rectangle rect = new Rectangle(735, 0, 1175 - 735, 1080);
            Bitmap search = Helper.GetImageInImage(Helper.ScreenChecker.ScreenShot, rect);

            Rectangle leftUpRect = Helper.ScreenChecker.FindImage(search, leftUpCorner, 0.99f);
            Rectangle rightBottom = Helper.ScreenChecker.FindImage(search, rightBottomCorner, 0.99f);

            if (leftUpRect.Width > 0 && leftUpRect.Height > 0 && rightBottom.Width > 0 && rightBottom.Height > 0)
            {
                Rectangle cardRect = new Rectangle(leftUpRect.X, leftUpRect.Y, rightBottom.Right - leftUpRect.X, rightBottom.Bottom - leftUpRect.Y);
                ret = Helper.GetImageInImage(search, cardRect);
            }

            return ret;
        }
    }
}
